package com.zhengdongzulin.zuke.app.main

import android.view.View
import android.widget.FrameLayout
import com.just.agentweb.AgentWeb
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.activities.ActivitiesBean
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.base.BaseViewModel
import com.zhengdongzulin.zuke.common.AndroidInterface
import com.zhengdongzulin.zuke.databinding.ZkWebviewBinding

/**
 * jeff
 */
class ZKWebviewActivity : BaseActivity<BaseViewModel, ZkWebviewBinding>() {

    lateinit var mAgentWeb: AgentWeb

    override fun getLayoutResId(): Int = R.layout.zk_webview

    override fun initVM(): BaseViewModel = BaseViewModel()

    override fun initView() {
        initWeb()
        var web = mAgentWeb.getWebCreator().getWebView()
        web.overScrollMode = View.OVER_SCROLL_NEVER
    }

    private fun initWeb() {
        val url = intent.getStringExtra("url")
        var type = intent.getStringExtra("type")
        var title = intent.getStringExtra("title")

        if (!title.isNullOrEmpty()) {
            setTitleContent(title)
        }
        if (type.isNullOrEmpty()) {
            mAgentWeb = AgentWeb.with(this).setAgentWebParent(
                mView.contentView, FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT
                )
            ).useDefaultIndicator(color(R.color.orange_ff5200), dp2px(1f))
                .createAgentWeb()
                .ready()
                .go(url)
        } else {
            if ("活动".equals(type)) {
                setTitleContent("活动详情")
                var activeBean: ActivitiesBean.Bean =
                    intent.getSerializableExtra("activeBean") as ActivitiesBean.Bean

                mAgentWeb = AgentWeb.with(this).setAgentWebParent(
                    mView.contentView, FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT
                    )
                ).useDefaultIndicator(color(R.color.orange_ff5200), dp2px(1f))
                    .createAgentWeb()
                    .ready()
                    .get()

                var temp =
                    "<div style=\"font-size:18px;color:#333;margin-bottom:10px;\">${activeBean.title}</div>\n" +
                            "  <div style=\" display: flex;align-items: center;justify-content: space-between;color:#999;font-size:11px;margin-bottom:22px;\">\n" +
                            "   <div>${activeBean.etName}</div>\n" +
                            "    <div>${activeBean.ct}</div>\n" +
                            "  </div>${activeBean.content}"

                var head = "<head>" +
                        "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                        "<style>*{padding:0;}img{max-width: 100%; width:auto; height:auto;}</style>" +
                        "</head>"

                mAgentWeb.urlLoader.loadDataWithBaseURL(
                    null,
                    "<html>${head}<body>${temp}</body></html>",
                    "text/html; charset=utf-8",
                    "utf-8",
                    null
                );
                return
            }
            setTitleContent(type)
            mAgentWeb = AgentWeb.with(this).setAgentWebParent(
                mView.contentView, FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT
                )
            ).useDefaultIndicator(color(R.color.orange_ff5200), dp2px(1f))
                .createAgentWeb()
                .ready()
                .go("file:///android_asset/${if (type == "隐私条款") "privacy.html" else "agreenment.html"}")
        }
    }

    override fun initData() {
        if (intent.getBooleanExtra(
                "isGone",
                false
            )
        ) mView.titleLayout.titleLayout.gone() else mView.titleLayout.titleLayout.visible()
        mView.titleLayout.back.setOnClickListener {
            if (!mAgentWeb.back()) super.onBackPressed()
        }
    }

    override fun initListener() {
        val androidInterface = AndroidInterface(mAgentWeb, this)
        mAgentWeb.jsInterfaceHolder.addJavaObject("android", androidInterface)
    }

    override fun onBackPressed() {
        if (!mAgentWeb.back()) super.onBackPressed()
    }

    override fun startObserve() {

    }

    override fun onPause() {
        mAgentWeb.getWebLifeCycle().onPause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mAgentWeb.getWebLifeCycle().onResume()
    }

    override fun onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy()
        super.onDestroy()
    }
}